package cn.edu.nuc.film.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.edu.nuc.film.common.PageControl;
import cn.edu.nuc.film.common.Pager;
import cn.edu.nuc.film.entity.FilmCity;
import cn.edu.nuc.film.entity.Screens;
import cn.edu.nuc.film.service.interfaces.FilmCityService;
import cn.edu.nuc.film.service.interfaces.ScreensService;

@Controller
@RequestMapping(value="/film/screens")
public class ScreensController {
	
	@Autowired
	private ScreensService screensService;
	
	@Autowired
	private FilmCityService fcService;
	
	/**
	 * 进入添加影厅界面(为影城添加影厅)
	 * @return
	 */
	@RequestMapping(value="/toadd",method=RequestMethod.GET)
	public String toAddScreens( int fcId,Model model){
		
		FilmCity fc = fcService.detail(fcId);
		model.addAttribute("filmCity", fc);
		return "/sys/screens/add";
		
	}
	
	/**
	 * 执行添加影厅操作
	 * @param screens
	 * @return
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String addScreens( Screens screens){
		
		screensService.addScreens(screens);
		return "redirect:list";
		
	}
	
	
	
	
	
	/**
	 * 获取某影城的影厅(通过fcId来获取)
	 * @param fcId
	 * @return
	 */
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public String screensList( String sname,HttpServletRequest request){
		
		//获取要查询的影ting名字
		//String sname = request.getParameter("sname");
		if( sname == null || sname.equals("")){
			sname = null;
		}
		
		//获取影城id
		int fcId = 2;
		//设置分页信息
		PageControl pc = new PageControl();
				
		//设置初始当前页currindex = 1
		Integer currindex = 1;
				
		// 如果获取的从前台获取的index(当前页)不为空，则赋值给 currindex(分页当前页)
		if(request.getParameter("index")!=null){
			currindex = Integer.parseInt(request.getParameter("index"));
		}
				
		//将获取的当前页设置在分页信息(PageControl)里
		pc.setCurrentindex(currindex);
		pc.setPagesize(5);
				
		//调用service中的分页方法，获取分页信息
		Pager<Screens> pager = screensService.list(pc, sname,fcId);
		
		//获取影城信息，传到前台页面，显示
		FilmCity filmCity = fcService.detail(fcId);
				
		
		//将分页信息传送到前台
		request.setAttribute("pager", pager);
		request.setAttribute("filmCity", filmCity);		
		//返回list页面
		return "/sys/screens/list";
		
	}
	
	/**
	 * 修改影厅信息(进入修改界面)
	 * @param screensid
	 * @return
	 */
	@RequestMapping(value="/toedit",method=RequestMethod.GET)
	public String toUpdateScreens( int screensid,Model model){
		
		Screens screens = screensService.detail(screensid);
		System.out.println("dfdf"+screens);
		model.addAttribute("screens", screens);
		return "/sys/screens/edit";
		
	}
	
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	public String updateScreens( Screens screens){
		
		screensService.updateScreens(screens);
		return "redirect:list";
		
	}
	
	/**
	 * 删除某影城的影厅
	 * @param screensid
	 * @return
	 */
	@RequestMapping(value="/delete",method=RequestMethod.GET)
	public String deleteScreens(int screensid){
		
		screensService.deleteScreens(screensid);
		return "redirect:list";
		
	}
	
	
	
}
